package com.exia.android.socialnet.twitter;

import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;
import twitter4j.DirectMessage;
import twitter4j.Paging;
import twitter4j.ProfileImage;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;



public class twit extends Activity{

	/**
	 * @param args
	 */
	private Twitter twitter = new TwitterFactory().getInstance();
	private RequestToken requestToken = null;
	private AccessToken accessToken = null;
	private String retour = "";
	private String token = "";
	private String secretToken = "";
	private SharedPreferences twitterSettings;
	private List<Status> statuses;
	private List<DirectMessage> messages;
	
	
	public twit(SharedPreferences preferences)
	{
		twitterSettings = preferences;
	}
	
	public String get_url() throws TwitterException
	{
		twitter.setOAuthConsumer("MGEmIrDSQYcoj217khPv6w", "CZ3N2Iz4O4USPx8hSGRArMuMi40ej3IdBN1QwaULA0");
		requestToken = twitter.getOAuthRequestToken();
		
		//on renvoie l'url de connexion
		if (null == accessToken)
		{
			retour = requestToken.getAuthorizationURL();
		}
		else
		{
			retour = "Probleme de connexion";
		}
		
		return retour;
	}
	   
	public void set_connexion(String pin) throws Exception
	{
		try
    	{
    		if(pin.length() > 0)
    		{
    			accessToken = twitter.getOAuthAccessToken(requestToken, pin);
    			twitter.setOAuthAccessToken(accessToken);
    			this.setPreferences(true);
    		}
    		else
    		{
    			this.getPreferences();
    			accessToken = new AccessToken(token, secretToken);
    			twitter.setOAuthAccessToken(accessToken);
    		}
    	} 
    	catch (TwitterException te) 
    	{
    		if(401 == te.getStatusCode())
    		{
    			System.out.println("Unable to get Token");
    		}
    		else
    		{
    			te.printStackTrace();
    		}
    	}
	    
	};
	
	public String set_status(String kbd_status)
	{
		
		try 
		{
			Status status = twitter.updateStatus(kbd_status);
			retour = "Status mis à jour : " + status.getText();
		} 
		catch (TwitterException e) 
		{
			retour = e.toString();
		}
		return retour;
	}
	
	public Boolean getPreferences(SharedPreferences twitterSettings)
	{
		boolean settings;
		getPreferences();
		
	    if(token == "vide")
	    {
	    	settings = false;
	    }
	    else
	    {
	    	settings = true;
	    }
		return settings;
	}
	
	public void setPreferences(boolean ecrire) throws IllegalStateException, TwitterException
	{
		if(ecrire == true)
		{
			twitterSettings.edit().putString("twitterUser", twitter.getScreenName()).commit();
			twitterSettings.edit().putString("token", accessToken.getToken()).commit();
			twitterSettings.edit().putString("secretToken", accessToken.getTokenSecret()).commit();
		}
		else if(ecrire == false)
		{
			twitterSettings.edit().remove("twitterUser").commit();
			twitterSettings.edit().remove("token").commit();
			twitterSettings.edit().remove("secretToken").commit();
		}
	}
	
	public void getPreferences()
	{
		token = twitterSettings.getString("token", "vide");
		secretToken = twitterSettings.getString("secretToken", "vide");
	}
	
	public String[] getStrTimeline() throws TwitterException
	{
		statuses = twitter.getFriendsTimeline();
		String[] strListTweet = new String[20];
		int n = 0;
		
		for(Status status : statuses)
		{
			strListTweet[n] = (status.getUser().getScreenName() + " : " + status.getText());
			n++;
		}
		
    	return strListTweet;
	}
	
	public ProfileImage[] getImagesProfiles() throws TwitterException
	{
		statuses = twitter.getFriendsTimeline();
		ProfileImage[] strListTweet = new ProfileImage[20];
		int n = 0;
		
		for(Status status : statuses)
		{
			strListTweet[n] = (twitter.getProfileImage(status.getUser().getScreenName().toString(), null));
			
			n++;
		}
		
    	return strListTweet;
	}
	
	public String getUserScreenname(String itemList)
	{
		String retour = "";
		for(Status status : statuses)
		{
			if(itemList.compareTo(status.getText()) > -1)
			{
				retour = status.getUser().getScreenName();
			}
		}
		return retour;
	}
	
	public String sendMsg(String recipient, String message)
	{
		String retour = "";

		try 
		{
			twitter.sendDirectMessage(recipient, message);
			retour = "Message envoyé à " + recipient + " : " + message;
		} 
		catch (TwitterException e) 
		{
			retour = e.toString();
			if(retour.contains("403"))
			{
				retour = "Vous ne pouvez envoyer deux fois le même message.";
			}
		}
		
		return retour;
	}
	
	public ProfileImage getUserImage(String screenName) throws TwitterException
	{
		ProfileImage profileImage = null;
		profileImage = twitter.getProfileImage(screenName, null);
		
		return profileImage;
	}
	
	public String[] getMessages()
	{
		Paging paging = new Paging(1);
		String[] retour = new String[30];
		
		int i = 0;
		int n = 0;
		
		while(i < 30)
        {
        	retour[i] = "";
        	i++;
        }
		
		
		try
		{
            messages = twitter.getDirectMessages(paging);
            for (DirectMessage message : messages)
            {
            	
                		retour[n] = (message.getSenderScreenName() + " à écrit : " + message.getText());
                	
            		n++;
            	
            }
		}
		
		catch (TwitterException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retour;
	}
}

